﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace frm_medic
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr);
            try
            {

                con.Open();
                string sql = "select inv from inv where drug='" + comboBox1.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Connection = con;
                SqlDataReader da = cmd.ExecuteReader();
                while (da.Read())
                {
                    textBox1.Text = da[0].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {

                con.Close();

            }
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlCommand sqlCommand2 = new SqlCommand();
                sqlCommand.Connection = con;
                sqlCommand2.Connection = con;
                sqlCommand.CommandText = "SELECT * FROM drug;";
                sqlCommand2.CommandText = "SELECT * FROM drug WHERE drug_id =@drug_id;";
                sqlCommand2.Parameters.AddWithValue("@drug_id", "1001");
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                sqlDataAdapter.SelectCommand = sqlCommand;
                DataTable drug = new DataTable();
                con.Open();
                sqlDataAdapter.Fill(drug);
                this.comboBox1.DataSource = drug;
                this.comboBox1.DisplayMember = "drug";
                this.comboBox1.ValueMember = "drug";
                SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    this.comboBox1.Text = sqlDataReader["drug"].ToString();
                }
                sqlDataReader.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }
    }
}
